package com.htfyun.factorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.android.server.roobo.RobotNVRomDataLocalJni;
import com.htfyun.factorytest.AgingTestActivity;
import com.htfyun.factorytest.config.HtfyunBuildConfig;
import com.htfyun.factorytest.config.NVRomKeys;

import com.htfyun.factorytest.R;

import com.htfyun.factorytest.utils.DebugLog;

public enum EngineerTestItemsEnum {
    screen(NVRomKeys.ID_SCREEN, NVRomKeys.KEY_TEST_SCREEN, true, R.string.test_screen, R.string.test_screen_en, ScreenActivity.class),
    finger_tp(NVRomKeys.ID_FINGER_TP, NVRomKeys.KEY_TEST_FINGER_TP, true, R.string.test_finger_tp, R.string.test_finger_tp_en, TouchDrawActivity.class),
    electric_tp(NVRomKeys.ID_ELECTRIC_TP, NVRomKeys.KEY_TEST_ELECTRIC_TP, true, R.string.test_electric_tp, R.string.test_electric_tp_en, TouchDrawActivity.class),
    key_and_led(NVRomKeys.ID_KEY_AND_LED, NVRomKeys.KEY_TEST_KEYS_AND_LED, true, R.string.test_key_and_led, R.string.test_key_and_led_en, KeyAndLedActivity.class),
    main_mic(NVRomKeys.ID_MAIN_MIC, NVRomKeys.KEY_TEST_MAIN_MIC, true, R.string.test_main_mic, R.string.test_main_mic_en, RecorderActivity.class),
    wakeup(NVRomKeys.ID_WAKEUP, NVRomKeys.KEY_TEST_WAKEUP, true, R.string.test_wakeup, R.string.test_wakeup_en, WakeupActivity.class),
    pen_write(NVRomKeys.ID_PEN_WRITE, NVRomKeys.KEY_TEST_PEN_WRITE, true, R.string.test_pen_write, R.string.test_pen_write_en, PWActivity.class),
    battery(NVRomKeys.ID_BATTERY, NVRomKeys.KEY_TEST_BATTERY, false, R.string.test_battery, R.string.test_battery_en, BatteryActivity.class),
    bluetooth(NVRomKeys.ID_BLUETOOTH, NVRomKeys.KEY_TEST_BT, true, R.string.test_bluetooth, R.string.test_bluetooth_en, BTActivity.class),
    wifi(NVRomKeys.ID_WIFI, NVRomKeys.KEY_TEST_WIFI, true, R.string.test_wifi, R.string.test_wifi_en, WifiActivity.class),
    pen_calib(NVRomKeys.ID_PEN_CALIB, NVRomKeys.KEY_TEST_PEN_CALIB, false, R.string.test_pen_calib, R.string.test_pen_calib_en, WacomCaliActivity.class),
    aging(NVRomKeys.ID_AGING, NVRomKeys.KEY_TEST_AGING, false, R.string.test_aging, R.string.test_aging_en, AgingTestActivity.class),
    ;
    private final int identify;
    private final String nvromKey;
    private final int nameResId;
    private final int nameEnResId;
    private final boolean keepTestAfterAging;
    private final Class<? extends Activity> ItemClass;
    private EngineerTestItemsEnum(int identify, String nvromKey, boolean testAfterAging, int nameResId, int nameEnResId, Class<? extends Activity> cls) {
        this.identify = identify;
        this.nvromKey = nvromKey;
        this.keepTestAfterAging = testAfterAging;
        this.nameResId = nameResId;
        this.nameEnResId = nameEnResId;
        ItemClass = cls;
    }

    public int getIdentify() {
        return identify;
    }

    public String getNVRomKey() {
        return nvromKey;
    }

    public String getNVRomKeyForAfterAging() {
        return nvromKey + "_afterAging";
    }
    public String getNVRomKeyForLog() {
        return nvromKey + "_log";
    }

    public boolean isKeepTestAfterAging() {
        return keepTestAfterAging;
    }
    public int getNameResId() {
        return nameResId;
    }

    public int getNameEnResId() {
        return nameEnResId;
    }

    public Class<? extends Activity> getItemClass() {
        return ItemClass;
    }

    private final static String SHARED_PREFERENCES_NAME = "factoryTestItems";

    public String getResult(Context context) {

        String key = getNVRomKey();
        return doGetResult(context, key);
    }

    public void setResult(Context context, String result) {

        String key = getNVRomKey();

        doSetResult(context, key, result);
    }

    public String getResultForAfterAging(Context context) {

        String defResult = "Pass";
        if (context != null) {
            defResult = context.getResources().getString(R.string.test_result_pass);
        }
        boolean keepTestAfterAging = isKeepTestAfterAging();
        if (!keepTestAfterAging) {
            return defResult;
        }

        String key = getNVRomKeyForAfterAging();

        return doGetResult(context, key);

    }

    public void setResultForAfterAging(Context context, String result) {

        boolean keepTestAfterAging = isKeepTestAfterAging();
        if (!keepTestAfterAging) {
            return;
        }
        String key = getNVRomKeyForAfterAging();

        doSetResult(context, key, result);

    }

    public String getResultForLog(Context context) {

        String key = getNVRomKeyForLog();

        return doGetResult(context, key);

    }

    public void setResultForLog(Context context, String log) {

        String key = getNVRomKeyForLog();

        doSetResult(context, key, log);

    }

    private String doGetResult(Context context, String key) {

        String defResult = "MA";
        if (context != null) {
            defResult = context.getResources().getString(R.string.test_result_unknown);
        }

        if (HtfyunBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            String result = RobotNVRomDataLocalJni.getInstance().getStringNVRom(key);

            if (TextUtils.isEmpty(result)) {
                result = defResult;
            }

            return result;

        } else {

            if (context == null) {
                return "";
            }

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);

            return sp.getString(key, defResult);
        }

    }

    private void doSetResult(Context context, String key, String result) {

        DebugLog.e("doSetResult key = " + key + ", result = " + result);

        if (HtfyunBuildConfig.SAVE_TEST_RESULT_INTO_NVROM) {

            RobotNVRomDataLocalJni.getInstance().setStringNVRom(key, result);

        } else {

            if (context == null) {
                return;
            }

            SharedPreferences sp = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();

            editor.putString(key, result);
            editor.apply();
        }
    }


    public boolean isPassed(Context context) {
        return isPassedWithResult(context, getResult(context));
    }

    public boolean isPassedForAfterAging(Context context) {
        return isPassedWithResult(context, getResultForAfterAging(context));
    }


    private boolean isPassedWithResult(Context context, String result) {
        String pass = "Pass";
        if (context != null) {
            pass = context.getString(R.string.test_result_pass);
        }
        return !TextUtils.isEmpty(result) && !TextUtils.isEmpty(pass) && result.equalsIgnoreCase(pass);
    }


    /**
     * get item enum by id
     * @param id identify {@link #identify}
     * @return
     */
    public static EngineerTestItemsEnum getItemEnumByIdentify(final int id) {
        for (EngineerTestItemsEnum itemsEnum : values()) {
            if (itemsEnum.getIdentify() == id) {
                return itemsEnum;
            }
        }
        return null;
    }

}
